package com.wendu.oa.leave.service.leave.impl;

import com.wendu.oa.common.exception.BusinessException;
import com.wendu.oa.leave.dao.leave.ExamDao;
import com.wendu.oa.leave.dto.LeaveDto;
import com.wendu.oa.leave.service.leave.ExamService;
import com.wendu.oa.service.page.PageUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Service @Transactional
@CacheConfig(cacheNames = "service.leave")
public class ExamServiceImpl implements ExamService {

    @Resource
    private ExamDao examDao;

    @Cacheable(key = "targetClass+'.'+methodName+'('+#p0+')'")
    @Override
    public Map<String,Object> getAppList(LeaveDto leaveDto) {
        return PageUtils.getPage(leaveDto,()->examDao.findAppList(leaveDto));
    }

    @CacheEvict(allEntries = true)
    @Override
    public void passLeave(LeaveDto leaveDto) {
        //（1）根据提交数据，判断当前用户是否为假条的申请人所在部门的领导
        boolean is = examDao.findIsLeaderOfLeave(leaveDto);
        if(!is){
            throw new BusinessException("不允许审批权限之外的假条！");
        }

        //（2）假条状态是否为待审批状态（状态值：1）
        boolean ok = examDao.findIsToExam(leaveDto);
        if(!ok){
            throw new BusinessException("不允许审批已审批过的假条！");
        }

        //（3）修改假条信息：状态置3、设置审批时间、填入审批意见、填入审批人（当前用户工号+姓名）
        leaveDto.setL_sp_date(new Date());
        examDao.updateLeavePass(leaveDto);
    }

    @CacheEvict(allEntries = true)
    @Override
    public void backLeave(LeaveDto leaveDto) {
        //（1）根据提交数据，判断当前用户是否为假条的申请人所在部门的领导
        boolean is = examDao.findIsLeaderOfLeave(leaveDto);
        if(!is){
            throw new BusinessException("不允许审批权限之外的假条！");
        }

        //（2）假条状态是否为待审批状态（状态值：1）
        boolean ok = examDao.findIsToExam(leaveDto);
        if(!ok){
            throw new BusinessException("不允许审批已审批过的假条！");
        }

        //（3）修改假条信息：状态置3、设置审批时间、填入审批意见、填入审批人（当前用户工号+姓名）
        leaveDto.setL_sp_date(new Date());
        examDao.updateLeaveBack(leaveDto);
    }
}
